package com.zw.admin.server.service.impl;

import com.zw.admin.server.dao.DelayTimeDao;
import com.zw.admin.server.dao.EarlyExitTimeDao;
import com.zw.admin.server.dao.GuardDao;
import com.zw.admin.server.model.DelayTime;
import com.zw.admin.server.model.Device;
import com.zw.admin.server.model.EarlyExitTime;
import com.zw.admin.server.model.Guard;
import com.zw.admin.server.service.GuardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GuardServiceImpl implements GuardService {

    @Autowired
    private GuardDao guardDao;
    @Autowired
    private DelayTimeDao delayTimeDao;
    @Autowired
    private EarlyExitTimeDao earlyExitTimeDao;

    @Override
    public List<String> getAllOrgName() {
        return guardDao.getAllOrgName();
    }

    @Override
    public List<Guard> getGuardInfoByOrg(String orgName) {
        return guardDao.getGuardInfoByOrg(orgName);
    }

    @Override
    public String updateDelayTiem(String delayTime) {
        List<DelayTime> delayTimes = delayTimeDao.selectAll();
        for (DelayTime time : delayTimes) {
            delayTimeDao.delete(time);
        }
        DelayTime insertTime = new DelayTime();
        insertTime.setDelayTime(delayTime);
        delayTimeDao.insert(insertTime);
        return "success";
    }

    @Override
    public String updateEarlyExitTime(String delayTime) {
        List<EarlyExitTime> earlyExitTimes = earlyExitTimeDao.selectAll();
        for (EarlyExitTime time : earlyExitTimes) {
            earlyExitTimeDao.delete(time);
        }
        EarlyExitTime insertTime = new EarlyExitTime();
        insertTime.setEarlyExitTime(delayTime);
        earlyExitTimeDao.insert(insertTime);
        return "success";
    }

    @Override
    public String getDelayTime() {
        return delayTimeDao.selectAll().get(0).getDelayTime();
    }

    @Override
    public String getEarlyExitTime() {
        return earlyExitTimeDao.selectAll().get(0).getEarlyExitTime();
    }

    @Override
    public List<Guard> getAllGuardInfo() {
        return guardDao.selectAll();
    }

    @Override
    public List<Guard> getGuardInfoByTime(String startTime, String endTime, String limit, String pageNum) {
        Integer start = (Integer.parseInt(pageNum) - 1) * Integer.parseInt(limit);
        Integer end = Integer.parseInt(limit);
        return guardDao.getGuardInfoByTime(startTime, endTime, start, end);
    }

    @Override
    public List<Guard> getGuardByTimeAndOrg(String startTime, String endTime, String limit, String pageNum, List<String> orgNames) {
        Integer start = (Integer.parseInt(pageNum) - 1) * Integer.parseInt(limit);
        Integer end = Integer.parseInt(limit);
        return guardDao.getGuardByTimeAndOrg(startTime, endTime, start, end, orgNames);
    }
}
